﻿#include "utils.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

//判断是不是数字
//static 
int my_is_digit2(char ch)
{
	return (ch >= '0') && (ch <= '9');
}

//字符串转换到unsinged int 
// internal ASCII string to unsigned int conversion
//static
unsigned long long int _atoull(const char* str)
{
	unsigned long long int i = 0U;
	while (my_is_digit2(*str))
	{
		i = i * 10U + (unsigned long long int)(*(str++) - '0');
	}
	return i;
}

//测试到64位失败
//int64_t my_atoll(const char* str)
//{
//  #ifdef _WIN32
//  char*pend;
//  int64_t t;
//  //return _atoi64(str);
//  t=  _atoi64(str);
//  printf("==%I64d",t);
//  return t;
//  #else
//  return atoll(str);    
//  #endif
//}
int64_t my_atoll(const char* str)
{
	int64_t e = 0LL;
	int64_t tmpKeep = 0LL;
	int i = 0;
	const int len = strlen(str);
	
	if (len <= 0) {
		goto EXIT_FUN;
	}

	if (str[0] == '-') {
		e = my_atoll(str + 1);
		e = (-e);
		goto EXIT_FUN;
	}
	
	for (i = 0; i < len; i++) {
		if (str[i] > '9' || str[i] < '0') {
			break;
		}
		tmpKeep = e;
		e *= 10;
		e += (((int64_t)(str[i] - '0')) & 0x00000000000000FF);
		if (tmpKeep > e) {
			/* 越界 */
			e = 0;
			goto EXIT_FUN;
		}
	}

EXIT_FUN:
	return e;
}